package com.gz.controller;

import com.gz.dto.TuserDto;
import com.gz.dto.TuserQueryDto;
import com.gz.entity.RoleInfo;
import com.gz.entity.Teacher;
import com.gz.entity.Tuser;
import com.gz.entity.UserRole;
import com.gz.service.TuserService;
import com.gz.service.UserRoleService;
import com.gz.vo.BaseRS;
import com.gz.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户表(Tuser)表控制层
 *
 * @author makejava
 * @since 2024-01-15 14:25:03
 */
@RestController
@RequestMapping("tuser")
@Api(value = "教师用户控制层", tags = {"教师用户控制层"})
public class TuserController {

  /**
   * 服务对象
   */
  @Autowired
  private TuserService tuserService;
  /**
   * 服务对象
   */
  @Autowired
  private UserRoleService userRoleService;


  @PostMapping(value = "/login")
  @ApiOperation(value = "用户登录接口", notes = "用户登录接口")
  @ApiResponses({
      @ApiResponse(code = 200, message = "OK", response = Tuser.class),
  })
  public BaseRS login(@RequestBody Tuser user, HttpServletRequest request,
      HttpServletResponse response) {
    TuserDto u = new TuserDto();
    u.setUsername(user.getUsername());
    List<Tuser> users1 = tuserService.queryAll(u);
    u.setPassword(user.getPassword());
    List<Tuser> users2 = tuserService.queryAll(u);
    if (CollectionUtils.isEmpty(users1)) {
      return BaseRS.builder().data(null).code("300").msg("未查找到该用户，请注册").build();
    } else {
      if (CollectionUtils.isEmpty(users2) || StringUtils.isEmpty(user.getPassword())) {
        return BaseRS.builder().data(null).code("301").msg("密码不能为空或密码错误").build();
      } else {
//        Tuser userBtk = new Tuser();
//        userBtk.setUsername(user.getUsername());
        Tuser tuser = users2.get(0);
        tuser.setPassword(null);
        /** 查询角色信息 */
        List<RoleInfo> roleInfos = tuserService.queryUserRole(tuser.getId() + "");
        List<Integer> roleIds = roleInfos.stream().map(a -> {
          return a.getId();
        }).distinct().collect(Collectors.toList());
        tuser.setRoles(roleInfos);
        tuser.setRoleIds(roleIds);
        return BaseRS.builder().data(tuser).code("200").msg("success,登录成功").build();
      }
    }
  }

  /**
   * 分页查询
   *
   * @param dto 筛选条件
   * @return 查询结果
   */
  @ApiOperation(value = "分页查询")
  @PostMapping("/queryPage")
  public R queryByPage(@RequestBody TuserDto dto, HttpServletRequest request) {
    return R.ok(tuserService.queryByPage(dto));
  }
  @ApiOperation(value = "条件查询教师用户")
  @PostMapping("/queryByParam")
  public R queryByParam(@RequestBody TuserDto dto, HttpServletRequest request) {
    return R.ok(tuserService.queryAll(dto));
  }

  /**
   * 通过主键查询单条数据
   *
   * @param id 主键
   * @return 单条数据
   */
  @ApiOperation(value = "主键查询")
  @GetMapping("/queryById")
  public R<Tuser> queryById(@RequestParam("id") Integer id) {
    return R.ok(tuserService.queryById(id));
  }

  /**
   * 通过主键查询单条数据
   *
   * @param realName 主键
   * @return 单条数据
   */
  @ApiOperation(value = "教师下拉搜索框")
  @GetMapping("/querySelByName")
  public R<List<Teacher>> querySelByName(@RequestParam("realName") String realName) {
    return R.ok(tuserService.querySelByName(realName));
  }
  /**
   * 通过主键查询单条数据
   *
   * @param roleId 主键
   * @return 单条数据
   */
  @ApiOperation(value = "根据角色id查询教学类用户")
  @GetMapping("/queryUserByRoleId")
  public R<?> queryUserByRoleId(@RequestParam("roleId") String roleId) {
    return R.ok(tuserService.queryUserByRoleId(roleId));
  }
  @ApiOperation(value = "查询所有教师用户")
  @GetMapping("/queryAllTeachers")
  public R<?> queryAllTeachers() {
    return R.ok(tuserService.queryUserByRoleId("2"));
  }
  @ApiOperation(value = "查询所有教师用户")
  @PostMapping("/queryPageTeachers")
  public R<?> queryPageTeachers(@RequestBody TuserQueryDto tuserDto) {
    tuserDto.setRoleId("2");
    tuserDto.setOrderField("crtDt");
    tuserDto.setOrderType("desc");
    return R.ok(tuserService.queryPageTeachers(tuserDto));
  }
  @ApiOperation(value = "下拉框查询所有教师用户")
  @GetMapping("/queryAllSelTeachers")
  public R<?> queryAllSelTeachers() {
    return R.ok(tuserService.querySelTUser("2"));
  }

  /**
   * 新增数据
   *
   * @param tuser 实体
   * @return 新增结果
   */
  @ApiOperation(value = "新增数据")
  @PostMapping("/save")
  public R<Tuser> add(@RequestBody Tuser tuser) {
    if(!StringUtils.isEmpty(tuser.getUsername())){
      TuserDto dto = new TuserDto();
      dto.setUsername(tuser.getUsername());
      List<Tuser> tusers = tuserService.queryAll(dto);
      if(CollectionUtils.isNotEmpty(tusers)){
        return R.failure("该用户名已被注册");
      }
    }
    tuser.setStatus(0);
    Tuser insert = tuserService.insert(tuser);
    List<Integer> roleIds = tuser.getRoleIds();
    List<UserRole> userRoles = new ArrayList<>();
    roleIds.forEach(a -> {
      UserRole userRole = new UserRole();
      userRole.setUserId(insert.getId());
      userRole.setRoleId(a);
      userRoles.add(userRole);
    });
    userRoleService.insertBatch(userRoles);
    return R.ok(insert);
  }

  /**
   * 编辑数据
   *
   * @param tuser 实体
   * @return 编辑结果
   */
  @ApiOperation(value = "编辑数据")
  @PostMapping("/update")
  public R<Tuser> edit(@RequestBody Tuser tuser) {
    return R.ok(tuserService.update(tuser));
  }

  /**
   * 删除数据
   *
   * @param id 主键
   * @return 删除是否成功
   */
  @GetMapping("/deleteById")
  @ApiOperation(value = "删除数据")
  public R<Boolean> deleteById(@RequestParam("id") Integer id) {
    return R.ok(tuserService.deleteById(id));
  }

}

